package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.apis.article.IArticleClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmNewsStatisticsDto;
import com.heima.utils.common.DateUtils;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.service.NewsStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class NewsStatisticsServiceImpl implements NewsStatisticsService {
    @Autowired
    private IArticleClient articleClient;
    /**
     * 查看文章统计
     * @param beginDate
     * @param endDate
     * @return
     */
    public ResponseResult getNewsDimension(String beginDate,String endDate) {
        Date begin = DateUtils.stringToDate(beginDate);
        Date end = DateUtils.stringToDate(endDate);
        Map<String,Object> map = new HashMap();
        //得到用户id
        Integer wmUserId = WmThreadLocalUtil.getUserID();
        ResponseResult responseResult = articleClient.getNewsDimension(wmUserId,begin,end);
        if (responseResult.getCode().equals(200)){
            String res = JSON.toJSONString(responseResult.getData());
            Map map1 = JSON.parseObject(res, Map.class);
            map.put("likesNum",map1.get("likes")==null? 0 : map1.get("likes"));
            map.put("collectNum",map1.get("collections") == null ? 0 : map1.get("collections"));
            map.put("publishNum",map1.get("countNum") == null ? 0 : map1.get("countNum"));
        }
        return ResponseResult.okResult(map);
    }

    /**
     * 分页展示文章列表
     * @param dto
     * @return
     */
    public PageResponseResult getNewsPage(WmNewsStatisticsDto dto) {
        Integer wmUserId = WmThreadLocalUtil.getUserID();
        dto.setWmUserId(wmUserId);
        PageResponseResult responseResult = articleClient.getNewsPage(dto);
        return responseResult;
    }
}
